//
//  JButtomView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/29.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JButtomView.h"
#import "JButton.h"

@implementation JButtomView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        _btnArr = [NSMutableArray array];
        [self setUpView];
    }
    return self;
}

- (void)setUpView {
    CALayer *line = [CALayer layer];
    line.backgroundColor = kRGBColor(227, 227, 227).CGColor;
    line.frame = CGRectMake(0, 0, kWindowW, 1);
    [self.layer addSublayer:line];
    
    NSArray *btnImg = @[@"fotinform",@"fotf",@"fotstar"];
    NSArray *btnName = @[@"消息",@"店铺",@"收藏"];
    
    CGFloat minBtnWid = (kWindowW/2.0)/3.0;
    
    for (NSInteger i = 0 ; i < 3; i++) {
        CGRect rect = CGRectMake(i*minBtnWid, 1, minBtnWid, self.height - 1);
        
        JButton *btn = [JButton buttonWithType:UIButtonTypeCustom
                                         frame:rect
                                         title:btnName[i]
                                    titleColor:kRGBColor(149, 149, 149)
                                     titleFont:12
                                 textAlignment:NSTextAlignmentCenter
                                         image:btnImg[i]
                          imageViewContentMode:UIViewContentModeCenter
                                           tag:i+1
                                      imageURL:nil
                                       handler:^(UIButton *sender) {
                                           if (_btnType) {
                                               _btnType(sender.tag);
                                           }
                                       }];

        
        [self addSubview:btn];
        [_btnArr addObject:btn];
    }
    
    CALayer *line1 = [CALayer layer];
    line1.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line1.frame = CGRectMake(minBtnWid - 0.5, 8, 1, 48 - 8*2);
    [self.layer addSublayer:line1];
    
    CALayer *line2 = [CALayer layer];
    line2.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line2.frame = CGRectMake(minBtnWid*2 - 0.5, 8, 1, 48 - 8*2);
    [self.layer addSublayer:line2];
    
    UIButton *btnCar = [UIButton buttonWithType:UIButtonTypeCustom];
    btnCar.frame = CGRectMake(3*minBtnWid, 1, kWindowW/4.0, self.height - 0.5);
    [btnCar setTitle:@"加入购物车" forState:UIControlStateNormal];
    [btnCar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnCar.titleLabel.font = kFONT_TITLE(12);
    btnCar.backgroundColor = kRGBColor(246, 175, 178);
    btnCar.tag = 4;
    [btnCar addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnCar];
    [_btnArr addObject:btnCar];
    
    UIButton *btnBuy = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBuy.frame = CGRectMake( 3 * minBtnWid + kWindowW/4.0, 1, kWindowW/4.0, self.height - 0.5);
    [btnBuy setTitle:@"立即购买" forState:UIControlStateNormal];
    [btnBuy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnBuy.titleLabel.font = kFONT_TITLE(12);
    btnBuy.backgroundColor = [UIColor redColor];
    btnBuy.tag = 5;
    [btnBuy addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnBuy];
    [_btnArr addObject:btnBuy];
}

- (void)btnClick:(UIButton *)btn
{
    if (_btnType) {
        _btnType(btn.tag);
    }
}

@end
